<?php


namespace app\admin\model;
use app\common\library\helper;
use app\common\model\Store as StoreModel;
use app\admin\service\Comment;
use app\admin\service\Goods;
use app\admin\service\Order;
use app\admin\service\User;
use think\Session;
use think\Db;
/**
 * 商城模型
 * Class Store
 * @package app\store\model
 */
class Store extends StoreModel
{
    /* @var Goods $GoodsModel */
    private $GoodsModel;

    /* @var Order $GoodsModel */
    private $OrderModel;

    /* @var User $GoodsModel */
    private $UserModel;

    /**
     * 构造方法
     */
    public function initialize()
    {
        parent::initialize();
        /* 初始化模型 */
        $this->GoodsModel = new Goods;
        $this->OrderModel = new Order;
        $this->UserModel = new User;
    }

    /**
     * 后台首页数据
     * @return array
     * @throws \think\Exception
     */
    public function getHomeData(){
        $lately7days = $this->getLately7days();
        //获取当前登录人所属权限的数据
        $info['widget-card']=[
            //下级总数量
            'service_oem'=>$this->getServiceCount(),
            //客户总量
            'service_user'=>$this->getKehu(),
        ];

        $data = [
            'widget-card' => $info['widget-card'],//客户总量
            'widget-echarts' => [
                'companyName'=>'众拓通',
                // 最近七天日期
                'date' => helper::jsonEncode($lately7days),
                'order_total' => helper::jsonEncode($this->getOrderTotalByDate($lately7days)),

            ]
        ];
        return $data;
    }
    /**
     * 最近七天日期
     */
    private function getLately7days(){
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }

    /**
     * 获取下级数据
     */
    private function getServiceCount(){
        //获取下级数据
        $sessionInfo=Session::get('yoshop_admin');
        $loginuserInfo=Db::name('love_admin_user')->where(['id'=>$sessionInfo['user']['id']])->find();
        if($loginuserInfo['areaid']==="0"){
            //主管理员
            return db('love_admin_user')->count()-1;
        }elseif ($loginuserInfo['areaid']==='all'){
            //城市管理员
            return db('love_admin_user')->where(['cityid'=>$loginuserInfo['cityid'],'type'=>['in','3,4,5']])->count();
        }else{
            //区域管理员
            return db('love_admin_user')->where(['areaid'=>$loginuserInfo['areaid'],'type'=>['in','4,5']])->count();
        }
    }
    /**
     * 自营获取公司数据
     */
    private function getServiceCount2($sessionInfo){
        //获取公司数据

        return db('yonghui_company')->where('status',1)->where('upId',$sessionInfo['user']['company_id'])->count();
    }

    /**
     *  获取客户数据
     */
    private function getKehu(){
        $sessionInfo=Session::get('yoshop_admin');
        $loginuserInfo=Db::name('love_admin_user')->where(['id'=>$sessionInfo['user']['id']])->find();
        if($loginuserInfo['type']==1){
            //主管理员
            $where['floweuserid']=['>',0];
        }elseif ($loginuserInfo['type']==2){
            //区域管理员
            //获取所有区域销售人员id
            $user=Db::name('love_admin_user')->where(['cityid'=>$loginuserInfo['cityid'],'type'=>['in','4,5']])->field('id,type,cityid')->select()->toArray();
            if($user){
                foreach ($user as $k=>$v){
                    $ids[]=$v['id'];
                }
            }
            $ids[]=$loginuserInfo['id'];
            $where['floweuserid']=['in',$ids];
        }else{
            $user=Db::name('love_admin_user')->where(['areaid'=>$loginuserInfo['areaid'],'type'=>['in','4,5']])->field('id,type,cityid')->select()->toArray();
            if($user){
                foreach ($user as $k=>$v){
                    $ids[]=$v['id'];
                }
            }
            $ids[]=$loginuserInfo['id'];
            $where['floweuserid']=['in',$ids];
        }
        return db('love_customer')->where($where)->count();
    }

    /**
     * 获取客户总量
     */
    private function getOrderTotal($day = null)
    {
        return number_format($this->OrderModel->getPayOrderTotal($day,$day));
    }

    /**
     * 获取客户总量 (指定日期)
     */
    private function getOrderTotalByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal($day);
        }
        return $data;
    }


}